var express = require('express');
var router = express.Router();
var formidable = require("formidable");

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

//详情页
router.get('/detail', function(req, res, next) {
  res.render('detail', { title: 'Express' });
});

//主题页
router.get('/boutique', function(req, res, next) {
  res.render('boutique', { title: 'Express' });
});

//应用墙
router.get('/category', function(req, res, next) {
  res.render('category', { title: 'Express' });
});

//搜索
router.get('/search', function(req, res, next) {
  res.render('search', { title: 'Express' });
});


//异步上传图片
router.post('/admin/uploadImg',function(req,res,next){
  var form = new formidable.IncomingForm(); //创建上传表单
  form.uploadDir ='../public/upload/temp';
  form.keepExtensions = true; //保留后缀
  form.maxFieldsSize = 4*1024*1024; //文件大小
  form.parse(req,function(err,fields,files){
    if(err){
      res.send("上传图片失败");
      return;
    }
    res.send("上传图片成功");
    // console.log(files.file.path)
  })
});
//同步上传图片
router.post('/admin/formpost', function(req, res, next) {
  var form = new formidable.IncomingForm();   //创建上传表单
  form.encoding = 'utf-8';        //设置编辑
  form.parse(req,function(err,fields,files){
    if(err){
      res.send("上传图片失败");
      return;
    }
    console.log(fields);
    console.log(files);
    res.send("上传图片成功");
  })
});

module.exports = router;
